<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 新闻控制器类
 * @author wwj
 * @version 1.0.1 2012-12-17
 */

class News extends MY_Controller
{
	function __construct()
	{
		parent::__construct();
		
		// 后台登录验证
		$this->check_login();
		
		// 验证权限
		$this->verify_purview(PURVIEW_NEWS);
		
		$this->load->model('news_m');

		// 加载文章的语言包
		$this->load->language('news');
		
		$this->validate_data = array(
				array('field' => 'title', 'label' => 'lang:news_title', 'rules' => 'trim|required|max_length[50]|xss_clean'),
				array('field' => 'author', 'label' => 'lang:news_author', 'rules' => 'trim|required|max_length[20]'),
				array('field' => 'keyword', 'label' => 'lang:com_keyword', 'rules' => 'trim|max_length[30]'),
				array('field' => 'classid', 'label' => 'lang:com_column_name', 'rules' => 'trim|required|is_natural_no_zero'),
				array('field' => 'source', 'label' => 'lang:news_source', 'rules' => 'trim|required|max_length[20]'),
				array('field' => 'releasename', 'label' => 'lang:com_releasename', 'rules' => 'trim|required|max_length[20]'),
				array('field' => 'picturepath', 'label' => 'lang:news_pictures', 'rules' => 'trim|max_length[200]'),
				array('field' => 'content', 'label' => 'lang:news_source', 'rules' => 'trim|required'),
				array('field' => 'remark', 'label' => 'lang:com_remark', 'rules' => 'trim|max_length[20]'),
				);
	}
	
	function index($page = '')
	{
		$this->manage($page);
	}
	
	/**
	 * 新闻列表显示
	 * @param int $page 当前显示页码
	 */
	function manage($page = '')
	{
		// 获取要显示的页数
		$page           = empty($page) ? 1 : abs(intval($page));
		
		// 组织分页信息
		$config['base_url']		= base_url('news/manage');
		$config['total_rows']	= $this->news_m->get_news_count();
		
		$page_data = $this->get_page($config);
		$data['page'] 	= $page_data['page'];
		$order 			= 'istop desc,isrecommend desc,id desc';

		// 查询新闻
		$news      = $this->news_m->show_news($page_data['limit'], $order);
		
		// 新闻信息列表数组
		$i = 1;
		foreach ($news as $key => $row)
		{
			$news[$key]['ordernum'] 	= ($page - 1) * $page_data['per_page'] + $i;
			$news[$key]['createtime']	=date(DATE_FORMAT,$row['createtime']);
			$news[$key]['updatetime']	=date(DATE_FORMAT,$row['updatetime']);
			$news[$key]['isactive'] 	= $row['isactive'] == 0 ? $this->lang->line('com_sign_inactive') : $this->lang->line('com_sign_active');
			$news[$key]['isverify'] 	= $row['isverify'] == 0 ? $this->lang->line('com_sign_not_verify') : $this->lang->line('com_sign_verify');
			$news[$key]['isrecommend'] = $row['isrecommend'] == 1 ? $this->lang->line('com_sign_recommend') : '';
			$news[$key]['istop'] 		= $row['istop'] == 1 ? $this->lang->line('com_sign_top') : '';
			$news[$key]['ispic'] 		= $row['ispic'] == 1 ? $this->lang->line('com_sign_picture') : '';
			$i++;
		}
		
		$data['header'] 		= $this->header;
		$data['title']			= $this->lang->line('news_title_list');
		$data['page_label']		= $this->lang->line('news_page_label');
		$data['nav_label'] 		= $this->lang->line('com_navigate_label');
		$data['news'] 			= $news;
		$this->load->view('news_list',$data);
	}
	
	/**
	 * 显示添加新闻页面
	 */
	function add()
	{
		$data['header'] 		= $this->header;
		$data['title']			= $this->lang->line('news_title_add');
		$data['page_label']		= $this->lang->line('news_page_label');
		$data['nav_label'] 		= $this->lang->line('com_navigate_label');
		$data['loginname'] 		= $this->loginname;
		$data['class'] 			= $this->get_column_as_array();
		$this->load->view('news_add', $data);
	}
	
	/**
	 * 添加新闻的业务处理
	 */
	function add_action()
	{
		$validate = $this->validation($this->validate_data);
		if ($validate !== TRUE)
		{
			$this->_show_message(base_url('news/manage'), $validate, 'goback');
		}
		
		$news = $this->input->post();
		$news_content = array();
		$news_picture = array();
		if ($news && isset($news['s_news_add']))
		{
			// 判断添加的类别是否是子类
			if (!$this->has_sub_column($news['classid']))
			{
				$this->_show_message(base_url('news/add'), $this->lang->line('error_choice_sub_column'), 'goback');
			}
			
			// 标记为图片新闻，并组建新闻图片数组
			if (!empty($news['picturepath']))
			{
				$news['ispic'] = 1;
				$news_picture['title'] = $news['title'];
				$news_picture['picturepath'] = $news['picturepath'];
			}
			
			$news['updatetime'] = $news['createtime'] = time();
			
			$news_content['content'] = $news['content'];
			$news_content['keyword'] = $news['keyword'];
			
			// 清除数组中无用的数据，添加相关信息
			unset($news['s_news_add']);
			unset($news['content']);
			unset($news['keyword']);
			unset($news['picturepath']);
			
			// 添加新闻
			$result = $this->news_m->insert_news($news, $news_content, $news_picture);
			if ($result)
			{
				$this->_show_message(base_url('news'), $this->lang->line('msg_operation_success'));
			}
			else
			{
				$this->_show_message(base_url('news/add'), $this->lang->line('msg_operation_failed'), 'goback');
			}
		}
		else
		{
			$this->_show_message(base_url('news'), $this->lang->line('error_invalid_post'));
		}
		
	}
	
	/**
	 * 显示新闻编辑页面
	 * @param int $id 新闻id
	 */
	function edit($id)
	{
		$news_id['news.id'] = intval($id);
		if (empty($news_id))
		{
			$this->_show_message(base_url('news'), $this->lang->line('news_id_unable_empty'), 'goback');
		}
		
		// 查询新闻信息
		$news = $this->news_m->get_news($news_id);
		if (empty($news))
		{
			$this->_show_message(base_url('news'), $this->lang->line('news_does_not_exist'), 'goback');
		}
		$news = $news[0];
		$data = array (
			
				'header' => $this->header,
				'news' => $news,
				'class' => $this->get_column_as_array(),
				'loginname'	=> $this->loginname,
				'title' => $this->lang->line('news_title_edit'),
				'page_label' => $this->lang->line('news_page_label'),
				'nav_label' => $this->lang->line('com_navigate_label'),
				'redirect' => $this->get_referer_url(),
		);
		$this->load->view('news_edit', $data);
	}
	
	/**
	 * 编辑新闻业务处理
	 */
	function edit_action()
	{
		$redirect = empty($_GET['redirect']) ? base_url('news') : $_GET['redirect'];
		$validate = $this->validation($this->validate_data);
		if ($validate !== TRUE)
		{
			$this->_show_message(base_url('news/manage'), $validate, 'goback');
		}
		
		$news = $this->input->post();
		$news_content = array();
		$news_picture = array();
		if ($news && isset($news['s_news_edit']))
		{
			// 判断添加的类别是否是子类
			if (!$this->has_sub_column($news['classid']))
			{
				$this->_show_message(base_url('news/add'), $this->lang->line('error_choice_sub_column'), 'goback');
			}
			
			// 新闻id不能为空
			if (empty($news['news_id']))
			{
				$this->_show_message(base_url('news'), $this->lang->line('news_id_unable_empty'), 'goback');
			}
			$news_id['newsid'] = $news['news_id'];
			
			// 判断原来的新闻是否是图片新闻
			if (1 == $this->news_m->picture_count($news_id))
			{
				
				// 编辑后，新闻的图片不为空，直接更新，否则删除原来的新闻图片及表记录
				if (!empty($news['picturepath']))
				{
					$news['ispic'] = 1;
					$news_picture['title'] = $news['title'];
					$news_picture['picturepath'] = $news['picturepath'];
				}
				else
				{
					// 删除新闻图片和新闻图片表中的记录，ispic标记为0;
					$news['ispic'] = 0;
					$this->news_m->delete_picture_by_newsid($news_id['newsid'], TRUE);
				}
			}
			else
			{
				// 原来不是图片新闻，编辑之后添加新闻图片。
				if (!empty($news['picturepath']))
				{
					$news_picture['newsid'] = $news['news_id'];
					$news_picture['title'] = $news['title'];
					$news_picture['picturepath'] = $news['picturepath'];
					$this->news_m->insert_news_picture($news_picture);
					$news_picture = array();
					$news['ispic'] = 1;
				}
				else
				{
					$news['ispic'] = 0;
				}
			}
			
			$news['updatetime'] = time();
			
			$news_content['content'] = $news['content'];
			$news_content['keyword'] = $news['keyword'];
			
			// 清除数组中无用的数据，添加相关信息
			unset($news['news_id']);
			unset($news['origin_classid']);
			unset($news['s_news_edit']);
			unset($news['content']);
			unset($news['keyword']);
			unset($news['picturepath']);
			
			// 更新新闻
			$result = $this->news_m->update_news(array('id' => $news_id['newsid']), $news);
			$this->news_m->update_news_content($news_id, $news_content);
			if (!empty($news_picture))
			{
				$result = $this->news_m->update_news_picture($news_id, $news_picture);
			}
			
			if ($result)
			{
				$this->_show_message($redirect, $this->lang->line('msg_operation_success'));
			}
			else
			{
				$this->_show_message(base_url('news'), $this->lang->line('msg_operation_failed'), 'goback');
			}
		}
		else
		{
			$this->_show_message(base_url('news'), $this->lang->line('error_invalid_post'));
		}
	}
	
	/**
	 * AJAX方式删除新闻，如果是图片新闻也删除其所属的图片，返回json信息
	 */
	function delete_action()
	{
		// 对表单数据做判断
		$validate = $this->validation(array(array('field' => 'id[]', 'label' => 'lang:news_id', 'rules' => 'trim|required|is_natural|xss_clean')));
		if (TRUE !== $validate)
		{
			echo $this->array2json(TRUE, $validate);
			return ;
		}
		
		// 获取文章ID值，并放入数组
		$news_id['id'] = $this->input->post('id');
		if ($this->news_m->delete_news($news_id))
		{
			echo $this->array2json(FALSE, $this->lang->line('msg_operation_success'));
			return ;
		}
		else
		{
			echo $this->array2json(TRUE, $this->lang->line('msg_operation_failed'));
			return ;
		}
	}
	
	/**
	 * 显示新闻状态设置页面
	 * @param int $id 新闻id
	 */
	function news_state($id)
	{
		$id = (int)$id;
		$news = $this->news_m->get_news_state(array('id' => $id), '1');
		if (empty($news))
		{
			$this->_show_message(base_url('news'), $this->lang->line('news_does_not_exist'), 'goback');
		}
		$news = $news[0];
		
		$data = array (
					
				'header' => $this->header,
				'state' => $news,
				'title' => $this->lang->line('news_title_state_config'),
				'model' => 'news'
		);
		$this->load->view('state_config', $data);
	}
	
	/**
	 * AJAX方式处理新闻状态设置业务
	 */
	function config_state()
	{
		echo parent::config_state('news_m', 'news');
		return ;
	}
}

/* End of file news.php */
/* Location: ./application/admin/controllers/news.php */